﻿using System;
using System.Net;
using System.Windows.Data;

namespace Alderamin.Helper
{
    [ValueConversion(typeof(WebProxy), typeof(string))]
    public class WebproxyToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            WebProxy proxy = (WebProxy)value;
            if (proxy != null)
                return proxy.Address.OriginalString;
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (value != null)
                {
                    WebProxy proxy = new WebProxy((string)value);
                    return proxy;
                }
            }
            catch (UriFormatException) { }
            return null;
        }
    }
}
